package com.ruoyi.web.controller.dict;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ColumnParam;
import com.ruoyi.common.core.domain.GenTableColumn;
import com.ruoyi.common.core.domain.body.BaseRequestBody;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.json.JsonMap;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.generator.service.IGenTableColumnService;
import com.ruoyi.generator.service.IGenTableService;
import com.ruoyi.generator.service.TbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 阀体字典Controller
 *
 * @author he
 * @date 2021-04-16
 */
@RestController
@RequestMapping("/dict")
@Slf4j
public class DictBaseController extends BaseController {
    @Autowired
    private TbService tbService;
    @Autowired
    private IGenTableColumnService genTableColumnService;
    @Autowired
    private IGenTableService genTableService;
    /**
     * 查询阀体字典列表
     */
    @PreAuthorize("@ss.hasPermi('dict:base:list')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody BaseRequestBody baseRequestBody) {
        Page list = tbService.getPageColumnQuery(baseRequestBody.getTbName(),
                getColumnQueryList(baseRequestBody.getColumnParams()), getPage(baseRequestBody.getPage()));
        return getDataTable(list);
    }


    /**
     * 导出阀体字典列表
     */
    @PreAuthorize("@ss.hasPermi('dict:base:export')")
    @RequestMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestBody BaseRequestBody baseRequestBody) {
        List<Map<String, Object>> list = tbService.getListColumnQuery(baseRequestBody.getTbName(),
                getColumnQueryList(baseRequestBody.getColumnParams()));
        List<GenTableColumn> genTableColumns = genTableColumnService.selectExportByTableName(baseRequestBody.getTbName());
        ExcelUtil util = new ExcelUtil(list, genTableColumns);
        return util.tbExportExcel();
    }

    /**
     * 新增保存阀体字典
     */
    @PreAuthorize("@ss.hasPermi('dict:base:add')")
    @Log(title = "阀体字典", businessType = BusinessType.INSERT)
    @RequestMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(@RequestBody JsonMap jsonMap, String tbName) {

        Map<String, GenTableColumn> genTableColumnMap = genTableColumnService.selectMapByTableName(tbName);
        return toAjax(tbService.addSave(tbName, jsonMap, genTableColumnMap));
    }




    /**
     * 修改保存阀体字典
     */
    @PreAuthorize("@ss.hasPermi('dict:base:edit')")
    @Log(title = "阀体字典", businessType = BusinessType.UPDATE)
    @RequestMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(@RequestBody JsonMap jsonMap, String tbName) {
        Map<String, GenTableColumn> genTableColumnMap = genTableColumnService.selectMapByTableName(tbName);
        return toAjax(tbService.updateById(tbName, jsonMap, genTableColumnMap));
    }

    /**
     * 删除阀体字典
     */
    @PreAuthorize("@ss.hasPermi('dict:base:remove')")
    @Log(title = "阀体字典", businessType = BusinessType.DELETE)
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String tbName, String ids) {
        return toAjax(tbService.removeAll(tbName, ids));
    }

}
